/*--------------------------------------------------------------------------*
 *                               Driver
 *                              PFC15163
 *--------------------------------------------------------------------------*/

#define _RTC_MOUDEL_
#include "rtc85163.h"
#include "at24c.h"
#include "swiic.h"

extern swiic_cfg_type eep_iic;
#define rtc_iic       eep_iic

void RtcStart()
{
	uint8_t Data;
	Data = 0;
	
	sw_iic_chip_send_str(&rtc_iic, RTC_CHIP_ADDR, RTC_SUBA_ADDR_CON_1, &Data, 1);
}

void RtcStop()
{
	uint8_t Data;
	Data = CON_1_STOP_MASK;
	sw_iic_chip_send_str(&rtc_iic,RTC_CHIP_ADDR, RTC_SUBA_ADDR_CON_1, &Data, 1);
}

void RtcGetTime(TIME_Type *RtcTime)
{
	uint8_t Data[7];
	if(IIC_ACK != sw_iic_chip_rcv_str(&rtc_iic, RTC_CHIP_ADDR, RTC_SUBA_ADDR_TIME_SEC, Data, 7))
        return;
    
	RtcTime->sec   = ( ((Data[0] & 0x70) >> 4 ) *10) + (Data[0] & 0x0F); 
	RtcTime->min   = ( ((Data[1] & 0x70) >> 4 ) *10) + (Data[1] & 0x0F); 
	RtcTime->hour  = ( ((Data[2] & 0x30) >> 4 ) *10) + (Data[2] & 0x0F); 
	RtcTime->day   = ( ((Data[3] & 0x30) >> 4 ) *10) + (Data[3] & 0x0F);
    RtcTime->week  = ( ((Data[4] & 0x30) >> 4 ) *10) + (Data[4] & 0x0F);
	RtcTime->mon   = ( ((Data[5] & 0x10) >> 4 ) *10) + (Data[5] & 0x0F);
	RtcTime->year  = ( ((Data[6] & 0xF0) >> 4 ) *10) + (Data[6] & 0x0F);
	RtcTime->year += 2000;
}

void RtcSetTime(TIME_Type *RtcTime)
{
	uint8_t Data[8];
	uint8_t Cal;
	Data[0] = ((RtcTime->sec  / 10) << 4) + (RtcTime->sec  % 10);
    Data[1] = ((RtcTime->min  / 10) << 4) + (RtcTime->min  % 10);
	Data[2] = ((RtcTime->hour / 10) << 4) + (RtcTime->hour % 10);
	Data[3] = ((RtcTime->day  / 10) << 4) + (RtcTime->day  % 10);
	Data[4] = RtcTime->week & 0x07;
	Data[5] = ((RtcTime->mon  / 10) << 4) + (RtcTime->mon  % 10);
	Cal     = RtcTime->year % 100;
	Data[6] = ((Cal / 10) << 4) + (Cal % 10);
    sw_iic_chip_send_str(&rtc_iic, RTC_CHIP_ADDR, RTC_SUBA_ADDR_TIME_SEC, Data, 7);
}

